import os,sys,pathlib

sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))


import time
import httpx
from core.downloader import Downloader

from core.item import BookItem, ChapterItem, rebuild_db
from core.func_wrapper import *
from core.log import get_logger
from core.request_base import Request


logger = get_logger('test')

def read_html_file(file_path):
    with open(file_path,encoding='utf-8') as f:
        return f.read()

def test_rebuild_db():
    rebuild_db()


def test_retry_decorate():
    @retry(3, logger)
    def foo():
        raise Exception()
    for i in range(3):
        try:
            foo()
        except:
            pass


def test_request(url):
    req = Request()
    res = req.get(url)
    print(res.status_code)

def test_book_download(book,force):
    downloader=Downloader()
    downloader.progress_changed_callbacks.append(log_process)
    downloader.download_book(book,'Comics')

def test_reset_download_state(book):
    Spider.reset_book_download_state(BookItem.get(book))



if __name__ == '__main__':
    # test_rebuild_db()
    # down_chapter_html('https://cn.baozimh.com/search?q=%E6%96%97%E7%A0%B4%E8%8B%8D%E7%A9%B9','search.html')
    # test_parse_chapter()
    # test_spider("https://cn.baozimh.com/comic/zuoriqingkong-koudaiqiaokeli")
    # test_save_state()
    # test_load_state()
    # test_retry_decorate()
    # test_request("https://cn.baozimh.com/comic/zuoriqingkong-koudaiqiaokeli")
    # test_reset_download_state(BookItem.get(1))
    # test_book_download(2,False)
    # test_parse_and_download('https://cn.baozimh.com/comic/yirenzhixia-dongmantang')
    pass